﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_userManager : BaseListPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (role.RoleId != 1)
        {
            Response.Redirect("login.html");
        }
        PageSize = 20;
        TableName = "SystemUser"; Pk = "UserId";
        if (!this.IsPostBack)
        {
            PageIndex = 0;
            BindData(ref rptList);
            BindAspNetPager(ref Pager);
            BindDDL();
        }
    }

    private void BindDDL()
    {
        UtilityDataBind.DropDownListBindData<Role>(ref rolelist,
            RoleBLL.GetInstance().GetList(), "RoleName", "RoleId", "角色");
    }

    protected void Pager_PageChanged(object sender, EventArgs e)
    {
        
        PageIndex = Pager.CurrentPageIndex - 1;
        BindData(ref rptList);
    }

    public string getStatus(object status)
    {
        string result = "未知";
        int s;
        try
        {
            s = Convert.ToInt32(status);
        }
        catch 
        {
            return result;
        }
        if (s == 1)
        {
            result = "正常";
        }
        else
        {
            result = "<font color='red'>禁用</font>";
        }

        return result;
    }

    public string getRoleName(object uid)
    {
        string result = "未知";
        int id;
        try
        {
            id = Convert.ToInt32(uid);
        }
        catch  
        {
            return result;
        }
        
        var name = RoleBLL.GetInstance().GetEntity(r => r.RoleId == id);
        if (name != null)
            result = name.RoleName;
        return result;
    }

    //根据条件查询
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //---------------------根据角色查询...
        Condition = "";
        string id = this.rolelist.SelectedValue;

        if (!id.Equals("-1"))
        {
            Condition = Condition + " and RoleId = " + id;
        }
        PageIndex = 0;//新定位到查询集合的第一页
        BindData(ref rptList);
        BindAspNetPager(ref Pager);
    }

    //拉黑
    protected void btnAudit_Click(object sender, EventArgs e)
    {
        string[] id = SelectId();
        if (id != null)
        {
            foreach (var i in id)
            {
                int userid = Convert.ToInt32(i);
                var t = SystemUserBLL.GetInstance().GetEntity(r => r.UserId == userid);
                if (t.Status == 0)
                {
                    t.Status = 1;
                }
                else
                {
                    t.Status = 0;
                }
                SystemUserBLL.GetInstance().Update(t);
            }
            BindData(ref rptList);
            BindAspNetPager(ref Pager);
        }
    }

    //修改信息事件
    protected void btnChangeInfo_Click(object sender, EventArgs e)
    {
        string[] id = SelectId();
        if(id!=null)
            Response.Redirect("userInfoUpdate.aspx?id=" + id[0]);
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("userInfoAdd.aspx");
    }

    private string[] SelectId()
    {
        string[] id=null;
        string ids = "";
        try
        {
            ids = Request["ids"];
            if (ids.Length == 0)
            {
                UtilityMessageBox.Show(this, "未选择");
                return null;
            }
        }
        catch  
        {
            UtilityMessageBox.Show(this, "未选择");
            return null;
        }
        id = ids.Split(',');
        return id;
    }


    public int getManagerNum()
    {
        int result = 0;
        try
        {
            result = CompanyUserBLL.GetInstance().GetList().Count;
        }
        catch(Exception e)
        {
            result = 0;
        }
        return result;
    }

    public int getGeneralNum()
    {
        int result = 0;
        try
        {
            result = GeneralUserBLL.GetInstance().GetList().Count;
        }
        catch (Exception e)
        {
            result = 0;
        }
        return result;
    }

    public int getBusinessNum()
    {
        int result = 0;
        try
        {
            result = BusinessUserBLL.GetInstance().GetList().Count;
        }
        catch (Exception e)
        {
            result = 0;
        }
        return result;
    }

}